package com.lwlk.util.dir;

import com.lwlk.util.string.Lines;

import java.io.File;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class Dir {

    // 模仿Python的那个walk方法 MDZZ NOI包里有这个方法
    // MDZZ NOI 自带这个方法
    public static ArrayList<Node> walk(String root) {
        File r = new File(root);
        ArrayList<String> search = new ArrayList<>();
        ArrayList<Node> nodeList = new ArrayList<>();
        if(r.exists() && r.isDirectory()) search.add(root);
        for(int i = 0; i < search.size(); ++i) {
            nodeList.add(new Node(search.get(i)));
            search.addAll(nodeList.get(nodeList.size() - 1).dirs);
        }
        return nodeList;
    }

    // 路径信息
    private static class Node {
        public String name = "";
        public ArrayList<String> dirs = new ArrayList<>();
        public ArrayList<String> files = new ArrayList<>();
        public Node(String fileName) {
            File[] fs = new File(fileName).listFiles();
            if(fs != null) for(File f : fs) {
                if(f.isFile()) files.add(f.getAbsolutePath());
                if(f.isDirectory()) dirs.add(f.getAbsolutePath());
            }
            name = fileName;
        }

        public String toRichString() {
            return new Lines("name: ?", "dirs: ?", "files: ?")
                        .with("", "\n", "\n")
                        .content(name, dirs.stream().collect(Collectors.joining(",")), files.stream().collect(Collectors.joining(", ")));
        }
    }

    public static void main(String[] args) {

        ArrayList<Node> nodes = Dir.walk("L:\\desmume");

        nodes.forEach((n) -> System.out.println(n.toRichString()));
    }
}
